package club.hicode.daydayup.zookeeper.distribute4;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.thread.ThreadUtil;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.io.IOException;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;

/**
 * 树形测试
 *
 * @author Liu Chunfu
 * @date 2018-06-11 上午11:00
 **/
@Slf4j
public class TreeTest {

    @Test
    public void coreTest() throws IOException {
        ExecutorService executorService = Executors.newCachedThreadPool();

        for (int i = 0; i < 10; i++) {
            executorService.execute(() -> {
                executorService.execute(() -> {
                    try {
                        ZkDistrubuteLock lock = new ZkDistrubuteLock();
                        lock.lock();
                        log.info("=={}获取锁开始做。。。", lock.getCurrentPath());
                        ThreadUtil.sleep(2, TimeUnit.SECONDS);
                        lock.unlock();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                });
            });
        }

        System.in.read();
    }

    @Test
    public void test3() throws Exception {
        ZkDistrubuteLock lock = new ZkDistrubuteLock();
        lock.lock();
        log.info("获取锁开始做。。。");
        lock.unlock();
    }

    @Test
    public void test1() {
        Set<String> set = new HashSet<>();
        set.add("001");
        set.add("021");
        set.add("011");
        set.add("133");
        TreeSet<String> treeSet = new TreeSet(set);


        String first = treeSet.first();
        //System.out.println(first);

        String last = treeSet.last();

        System.out.println(treeSet);
        SortedSet<String> strings = treeSet.headSet("021");
        System.out.println(strings);
    }

    @Test
    public void test2() {
        List<Map<String, Object>> list = new ArrayList<>();
        Optional<Long> reduce = list.stream().filter(map -> map.get("value") != null)
                .map(Map::values) //提取所有的 values
                .flatMap(Collection::stream) // 将 values转为 stream
                .map(Convert::toLong) //转为 long
                .reduce(Long::sum); //求和

        Optional<Long> value = list.stream().map(map -> map.get("value")).map(Convert::toLong).reduce(Long::sum);

    }
}
